package com.lazyfish.superdir.controller;

import com.lazyfish.superdir.entity.*;
import com.lazyfish.superdir.service.DirService;
import com.lazyfish.superdir.service.StudentAllService;
import com.lazyfish.util.ResultBuild;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@CrossOrigin
@Controller
public class DirController {
    @Autowired
    private DirService dirService;
    @Autowired
    private StudentAllService studentAllService;
    @RequestMapping("/Dir_getTotal")
    @ResponseBody
    public ResultBuild getTotal(){
        return new ResultBuild(200,dirService.getTotal());
    }
    @RequestMapping("/Dir_makeDir")
    @ResponseBody
    public ResultBuild makeDir(Dir dir){
        File file=new File(FatherDir.getPath()+"/" + dir.getName());
        if(!file.exists()){//如果文件夹不存在
            file.mkdir();//创建文件夹
        }
        return new ResultBuild(200,dirService.makeDir(dir));
    }
    @RequestMapping("/Dir_modifyDir")
    @ResponseBody
    public ResultBuild modifyDir(Dir dir){
        return new ResultBuild(200,dirService.modifyDir(dir));
    }
    @RequestMapping("/Dir_updateCollection")
    @ResponseBody
    public ResultBuild updateCollection(int collect,int id){
        return new ResultBuild(200,dirService.updateDirCollection(collect,id));
    }
    @RequestMapping("/Dir_getDir")
    @ResponseBody
    public ResultBuild getDir(int id){
        return new ResultBuild(200,dirService.getDir(id));
    }
    @Value("${diyxitop.host}")
    String host;
    @Value("${diyxitop.password}")
    String password2;
    @Value("${diyxitop.user}")
    String user;
    @RequestMapping("/Dir_upload_BaiduCloud")
    @ResponseBody
    public ResultBuild upload_BaiduCloud(int id) throws IOException {
        Dir dir =new Dir();
        dir = dirService.getDir(id);
        UbuntuDiyxitop ubuntuDiyxitop = new UbuntuDiyxitop();
        ubuntuDiyxitop.setHost(host);
        ubuntuDiyxitop.setPort(22);
        ubuntuDiyxitop.setUser(user);
        ubuntuDiyxitop.setPassword(password2);
        System.out.println(ubuntuDiyxitop.getHost());
        // 主机1
        SSHRegisterEntity sshRegisterEntity1 = new SSHRegisterEntity(ubuntuDiyxitop);
        System.out.println("bypy syncup /root/testdir/"+dir.getName() +" /超级文件大师百度云备份/"+dir.getName());
        StringBuilder stringBuilder1 = sshRegisterEntity1.exec("bypy syncup /root/testdir/"+dir.getName() +" /超级文件大师百度云备份/"+dir.getName());
        return new ResultBuild(200,stringBuilder1);
    }
    @RequestMapping("/Dir_deleteDir")
    @ResponseBody
    public ResultBuild deleteDir(int id){
        return new ResultBuild(200,dirService.deleteDir(id));
    }
    @RequestMapping("/Dir_getDirList")
    @ResponseBody
    public ResultBuild Dir_getDirList(){
        return new ResultBuild(200,dirService.getDirList());
    }
    @RequestMapping("/Dir_test")
    @ResponseBody
    public ResultBuild test(int id) throws FileNotFoundException {
        File file=new File(FatherDir.getPath()+"\\test");
        if(!file.exists()){//如果文件夹不存在
            file.mkdir();//创建文件夹
        }
        return new ResultBuild(200,null);
    }
    @Value("${a}")
    String account;
    @Value("${p}")
    String password;
    @RequestMapping(value = "/Dir_login")
    @ResponseBody
    public ResultBuild login(User user, HttpSession session){
        if(user.getUsername().equals(account) && user.getPassword().equals(password)){
            //将用户对象添加到Session中
            session.setAttribute("USER_SESSION",user);
            session.setMaxInactiveInterval(30 * 60);
            return new ResultBuild(200,true);
        }
        return new ResultBuild(200,false);
    }
    @RequestMapping(value = "/Dir_logout")
    @ResponseBody
    public ResultBuild logout(HttpSession session){
        session.removeAttribute("USER_SESSION");
        return new ResultBuild(200,true);
    }
    @RequestMapping("/Dir_getNotHandList")
    @ResponseBody
    public ResultBuild Dir_getNotHandList(int id,Integer id2,int where){
        Dir dir = dirService.getDir(id);

        List<StudentAll> students = studentAllService.getStudentAllList();
        File file = new File(FatherDir.getPath()+"/"+dir.getName());
        File[] tempList = file.listFiles();
        Map<String, Integer> map = new HashMap<String, Integer>();
        Map<String, List<StudentAll>> res = new HashMap<>();
        for (int i = 0; i < tempList.length; i++) {
            if (tempList[i].isFile()) {
                String fileName = tempList[i].getName().split("\\.")[0].split("-")[where];
                map.put(fileName,1);
            }
            if (tempList[i].isDirectory()) {
                //这里就不递归了，
            }
        }
        if(id2!=null){
            Dir dir2 = dirService.getDir(id2);
            file = new File(FatherDir.getPath()+"/"+dir2.getName());
            tempList = file.listFiles();
            for (int i = 0; i < tempList.length; i++) {
                if (tempList[i].isFile()) {
                    String fileName = tempList[i].getName().split("\\.")[0].split("-")[where];
                    map.put(fileName,1);
                }
                if (tempList[i].isDirectory()) {
                    //这里就不递归了，
                }
            }
        }

        List<StudentAll> noHandStudents = new ArrayList<StudentAll>();
        List<StudentAll> HandStudents = new ArrayList<StudentAll>();
        for (StudentAll s:students) {
            if(map.get(s.getName())==null){
                noHandStudents.add(s);
            }else{
                HandStudents.add(s);
            }
        }
        res.put("noHand",noHandStudents);
        res.put("Hand",HandStudents);
        return new ResultBuild(200, res);
    }
}
